package com.gitbl.webchat.utils;

import java.util.Properties;
import groovyjarjarcommonscli.*;

public class ArgumentsHelper {
    public static Properties parseProperties(String[] commandArgs,String cmdDescription,String target) throws ParseException {

        Options options = new Options();
        @SuppressWarnings("static-access")
        Option property = OptionBuilder.withArgName("property=value")
                .hasArgs(2)
                .withValueSeparator()
                .withDescription(cmdDescription)
                .create("D");
        property.setRequired(true);
        options.addOption(property);
        // print usage

        //formatter.printHelp( "webchat-provider-user", options );
        System.out.println();
        // create the command line parser
        PosixParser parser = new PosixParser();
        if(commandArgs.length>0) {
            try {
                CommandLine cmd = parser.parse(options, commandArgs);
                Properties pps = cmd.getOptionProperties("D");
                for (Object p : pps.keySet()) {
                    System.out.println(p.toString() + "=" + pps.getProperty(p.toString()));
                    System.setProperty(p.toString(), pps.getProperty(p.toString()));
                }
                return pps;
            }catch (Exception ex){
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp(
                        "java -jar "+ target,
                        options);
                return null;
            }
        }
        return new Properties();
    }
}
